<?php
namespace Admin\Controller;

class ActorController extends AdminController
{
	//演员首页
	public function index ()
	{
		if (!empty($_POST)) {
			/*var_dump($_POST); exit;*/
			$list = $_POST['name'];
			$data['name'] = array('like','%'.$list.'%'); 
			//var_dump($data);
			
			/*var_dump($data);exit;*/
			$count = M('actor')->where($data)->count();
			$page = new \Think\Page($count,8);
			$show = $page->show();
			$list = M('actor')->where($data)->limit($page->firstRow.','.$page->listRows)->select();
		} else {
			$count = M('actor')->count();
			$page = new \Think\Page($count,8);
			$show = $page->show();
			$list = M('actor')->limit($page->firstRow.','.$page->listRows)->order('id asc')->select();
		}
		if(!$list) {
			$this->assign ('row','暂无搜索内容');
		}
		/*$page->setConfig('next','下一页');
		$this->assign('data',$data);*/
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	//演员添加页
	public function add ()
	{
		$this->display();
	}
	public function insert ()
	{
		/*var_dump($_GET); exit;*/
		$actor = D('actor');
		if (!$actor->create ($_GET)) {
			$this->error($actor->getError());
			
		} else {
			
			if ($actor->add () > 0) {
				$this->success ('添加成功',U ('Actor/index'));
			} else {
				$this->error ('添加失败');
			}
		}
	}
	//编辑页
	public function edit ()
	{
		$id = I ('get.id/d');
		$list = M ('actor')->find ($id);
		//var_dump($list);exit;
		$this->assign ('list',$list);
		$this->display ();
	}
	public function update ()
	{
		/*var_dump($_GET);
		exit;*/
		$actor = D ('actor');
		if (!$actor->create($_GET)) {
			$this->error($actor->getError());
			exit;
		} else {
			//var_dump($actor->create());exit;
			if ($actor->save()!==false) {
				$this->success('修改成功',U('Actor/index'));
			} else {
				$this->error('修改失败');
			}
		}
	}
	public function delete ()
	{
		$id = I ('get.id/d');
		$list = M('actor')->find($id);
		$file ="./Uploads/".substr($list['icon'],0,8).'/'.substr($list['icon'],8);
		/*var_dump($data);
		var_dump($file);exit;*/
		if (M ('actor')->delete ($id) && unlink ($file)) {
			$this->success ('删除成功',U ('Actor/index'));
		} else {
			$this->error ('删除失败');
		}
	}
	public function upload () 
	{
		$actor = D('actor');
		if (!$actor->create ($_GET)) {
			$this->error($actor->getError());
			exit;
		}
		$upload = new \Think\Upload ();
		$upload->maxSize = 3145728; //设置上传文件的大小
		$upload->exts = array ('jpg','gif','png','jpeg'); //设置文件的上传类型
		$upload->subName = array('date','Ymd'); //设置保存的子目录路径
		$upload->rootPath = './Uploads/'; //上传文件的根路径
		$upload->savePath = ''; //上传文件相对于根路径的保存路径
		$info = $upload->upload ();
		/*var_dump($info); exit;*/
		if (!$info) {
			$this->error($upload->getError());
		} else {
			$icon = date('Ymd').$info['icon']['savename'];
			/*var_dump($info); exit;*/
			$this->success('上传成功',U('Actor/insert',array('name'=>$_POST['name'],'icon'=>$icon)));
		}
	}
	public function upload1 () 
	{
		$actor = D('actor');
		if (!$actor->create ($_GET)) {
			$this->error($actor->getError());
			exit;
		}
		$upload = new \Think\Upload ();
		$upload->maxSize = 3145728; //设置上传文件的大小
		$upload->exts = array ('jpg','gif','png','jpeg'); //设置文件的上传类型
		$upload->subName = array('date','Ymd'); //设置保存的子目录路径
		$upload->rootPath = './Uploads/'; //上传文件的根路径
		$upload->savePath = ''; //上传文件相对于根路径的保存路径
		$info = $upload->upload ();
		/*var_dump($info); exit;*/
		if (!$info) {
			$this->error($upload->getError());
		} else {
			$icon = date('Ymd').$info['icon']['savename'];
			/*var_dump($info); exit;*/
			$this->success('上传成功',U('Actor/update',array('id'=>$_POST['id'],'name'=>$_POST['name'],'icon'=>$icon)));
		}
	}
}